from sanic import Blueprint, response
from services import user_service
from utils import result_util

# Blueprint 请求前缀
user_bp = Blueprint('user', url_prefix='/user')


# 获取所有用户
@user_bp.get('/list')
async def list_all(request):
    """获取所有用户"""
    users = await user_service.list_all()
    return response.json(result_util.ok(users))


# 分页获取所有用户
@user_bp.get('/page')
async def query_page(request):
    """分页查询用户"""
    page_users = await user_service.list_with_pagination(request.args)
    return response.json(page_users)


# 添加用户
@user_bp.post('/add_user')
async def add_user(request):
    """添加用户"""
    user = await user_service.create_user(request.json)
    return response.json(result_util.ok(user))


# 更新用户
@user_bp.post('/update_user')
async def update_user(request):
    """更新用户"""
    user = await user_service.update_user(request.json)
    return response.json(result_util.ok(user))


# 删除用户
@user_bp.get('/delete_user/<user_id>')
async def delete_user(request, user_id):
    """删除用户"""
    result = await user_service.delete_user(user_id)
    return response.json(result_util.flag(result))
